【Unity】基于MVC模式的背包系统 UGUI实现 您所在的位置:网站首页 mvc 模式 【Unity】基于MVC模式的背包系统 UGUI实现

【Unity】基于MVC模式的背包系统 UGUI实现

2023-03-14 23:00| 来源: 网络整理| 查看: 265

【Unity】基于MVC模式的背包系统 UGUI实现 发表于2018-01-06 评论0 955浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学堂游戏程序行业精英群

711501594 前言本文基于MVC模式,用UGUI初步实现了背包系统,想知道如何使用UGUI实现背包系统的可以看看。Control层包括了点击和拖拽两种逻辑。下载地址:https://github.com/duzixi/InventorySystem一、project准备(详见project文件)场景中Canvas上的对象:Bag:用于显示背包内容PickedItem:用于显示拾取道具的图片资源中的预设体:Item:生成背包的格子二、源码Model 层 using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; /// /// 脚本功能:MVC模式——Model层。定义物品结构,保存物品数据 /// 加入对象:Bag 背包(Canvas下的空对象) /// 版权声明:Copyright (c) 2015 duzixi.com All Rights Reserved /// 创建日期:2015.5.8 /// 知识要点: /// 1. MVC /// 2. 自己定义类 /// 3. 类的嵌套 /// public class ItemModel : MonoBehaviour { // 物品类的定义 public class Item { public string name; // 物品名称 public Sprite img; // 物品图片 // 构造器 public Item(string name, Sprite img) { this.name = name; this.img = img; } } public static List items; // 保存物品对象的集合 // 物品图片数组 public int size = 16; Sprite[] sprites; void Awake() // 数据初始化 { items = new List(); // 初始化List sprites = new Sprite[size]; // 依据行列值初始化物品列表 for (int i = 0; i < BagView.row; i++) { for (int j = 0; j < BagView.col; j++) { items.Add(new Item("", null)); } } // 【注意】实际开发中下面部分应由数据库取代 for (int i = 0; i < size; i++) { string name = i < 9 ? "0" + (i + 1) : "" + (i + 1); sprites[i] = Resources.Load(name, typeof(Sprite)) as Sprite; items[i] = new Item(" ", sprites[i]); } } } View 层 using UnityEngine; using UnityEngine.UI; using System.Collections; /// /// 脚本功能:MVC模式 —— View视图,控制背包的显示方式 /// 加入对象:Bag 背包 (Canvas下的空对象) /// 版权声明:Copyright (c) 2015 duzixi.com All Rights Reserved /// 创建时间:2015.05.08 /// 改动记录:2015.05.18 加入编号 /// 改动记录:2015.07.03 封装显示物品行子方法 /// 知识要点: /// 1. MVC /// 2. UGUI /// public class BagView : MonoBehaviour { // 背包规格 public static int row = 4; // 行 public static int col = 5; // 列 // 背包格子 public GameObject grid; float width; // 格子宽度 float height; // 格子高度 // 依据格子预设体获取宽和高 void Awake() { width = grid.GetComponent().rect.width; height = grid.GetComponent().rect.height; } // 初始状态:平铺格子。创建背包 void Start () { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { // 计算ID值(物品列表下标) int id = j + i * col; // 实例化格子预设,按宽高布局 GameObject itemGrid = Instantiate(grid, transform.position + new Vector3(j * width, -i * height, 0), Quaternion.identity) as GameObject; // 将实例化的格子对象设置为背包的子对象 itemGrid.transform.SetParent(transform); // 调用自己定义方法:显示某个id的格子内容 ShowItem(itemGrid.transform, id); // 给格子 PickUpDrop 组件编号。拾取放下时用 itemGrid.GetComponent().gridID = id; } } } // 又一次刷新背包显示(物品位置发生变化时) public void ShowItems() { for (int i = 0; i < row * col; i++) { Transform itemGrid = transform.GetChild(i); ShowItem(itemGrid, i); } } // 显示物品行子 private void ShowItem(Transform itemGrid, int id) { // 显示物品名称 Text txtUGUI = itemGrid.GetComponentInChildren(); txtUGUI.text = ItemModel.items[id].name; // 获取物品Icon的Image组件 Image imageUGUI = itemGrid.GetChild(0).GetComponent(); // 假设有物品。就显示图片 if (ItemModel.items[id].img != null) { imageUGUI.color = Color.white; } else { // 否则不显示 imageUGUI.color = Color.clear; } imageUGUI.sprite = ItemModel.items[id].img; } } using UnityEngine; using UnityEngine.UI; using System.Collections; /// /// 脚本功能:让拾取的背包物品随鼠标移动 /// 加入对象:PickedItem 拾取的物品 /// 版权声明:Copyright (c) 2015 duzixi.com All Rights Reserved /// 创建日期:2015.05.18 /// 改动记录:2015.07.03 加入射线忽略 /// 知识要点: /// 1. UGUI RectTransform、锚点、中心点 /// 2. 忽略射线接口 ICanvasRaycastFilter /// public class MoveWithMouse : MonoBehaviour, ICanvasRaycastFilter { RectTransform rect; // 获取UGUI定位组件 Image icon; // 显示当前拾取物品的图标 void Awake() { rect = GetComponent(); // 【注意】图标对象是第0个子对象 icon = transform.GetChild(0).GetComponent(); } void Update () { // 用鼠标位置给图标图片定位 rect.anchoredPosition3D = Input.mousePosition; // 依据是否有图片确定透明度 if (PickUpDrop.pickedItem.img != null) { icon.color = Color.white; } else { icon.color = Color.clear; } icon.sprite = PickUpDrop.pickedItem.img; } // 忽略鼠标图标上的射线 public bool IsRaycastLocationValid (Vector2 sp, Camera eventCamera) { return false; } } Control 层 using UnityEngine; using UnityEngine.EventSystems; using System.Collections; /// /// 脚本功能:MVC模式 —— Control控制,背包内物品摆放 /// 加入对象:Item 物品行子预设体 /// 版权声明:Copyright (c) 2015 duzixi.com All Rights Reserved /// 创建日期:2015.05.18 duzixi.com /// 改动记录:2015.07.03 加入射线忽略 /// 知识要点: /// 1. UGUI、MVC设计模式 /// public class PickUpDrop : MonoBehaviour, IDropHandler { public int gridID; public static ItemModel.Item pickedItem; // 当前拾取的物品 void Start () { // 初始化当前拾取物品为空 pickedItem = new ItemModel.Item("", null); } // 背包核心逻辑:交换 public static void SwapItem(int gridID) { // 交换背包中的物品和拾取物品 ItemModel.Item temp = pickedItem; pickedItem = ItemModel.items[gridID]; ItemModel.items[gridID] = temp; // 刷新背包显示 GameObject.Find("Bag").GetComponent().ShowItems(); } // 当物品button被点下时(点击触发模式) public void Drop() { SwapItem(gridID); } // 当物品放在格子中时(拖拽触发模式) public void OnDrop (PointerEventData eventData) { SwapItem(gridID); } } using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; /// /// 脚本功能:MVC模式 —— Control控制。操作逻辑。拖拽事件处理 /// 加入对象:Bag 背包 (Canvas下的空对象) /// 版权声明:Copyright (c) 2015 duzixi.com All Rights Reserved /// 创建时间:2015.07.03 /// 知识要点: /// 1. UnityEngine.EventSystem /// 2. IBeginDragHandlder, IDragHandler, IEndDragHander /// public class DragEvent : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler { int gridID = 0; // 格子编号 void Start() { gridID = GetComponentInParent().gridID; // 获取格子编号 } // 開始拖拽 public void OnBeginDrag (PointerEventData eventData) { // 调用交换方法 PickUpDrop.SwapItem(gridID); } // 拖拽中 public void OnDrag (PointerEventData eventData) { // 【注意】即使没有不论什么代码处理也要实现该接口。否则拖拽不成功 } // 结束拖拽 public void OnEndDrag (PointerEventData eventData) { // 调用交换方法 PickUpDrop.SwapItem(gridID); } } 后语眼下还有点Bug。拖拽模式下当前物体拖拽会导致物品尾随。另外屏幕适配还没有搞。

原文链接

著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签:

UGUIUnityList游戏视觉接口Github

本文作者

feng 暂无简介 Mesh绘制交互网格 Unity基本介绍与使用 Unity使用RenderTexture实现实时阴影绘制 Unity 贴图压缩方法及对比 Unity3D AsssetBundle加载效率比较 GWB公众号 腾讯游戏学堂公众号


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有